<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取指定主机的DNS记录</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.dns-get-record" class="refentry">
    <div class="refnamediv">
        <h1 class="refname">dns_get_record</h1>
        <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">dns_get_record</span> &mdash; <span class="dc-title">获取指定主机的DNS记录</span></p>

    </div>

    <div class="refsect1 description" id="refsect1-function.dns-get-record-description">
        <h3 class="title">说明</h3>
        <div class="methodsynopsis dc-description">
            <span class="methodname"><strong>dns_get_record</strong></span>
             ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$hostname</code></span>
            [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code><span class="initializer"> = DNS_ANY</span></span>
            [, <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$authns</code></span>
            [, <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$addtl</code></span>
            [, <span class="methodparam"><span class="type">bool</span> <code class="parameter reference">&$raw</code><span class="initializer"> = false</span></span>
        ]]]] ) : <span class="type">array</span></div>

        <p class="para rdfs-comment">
            获取指定主机（<code class="parameter">hostname</code>）的DNS记录。
        </p>
    </div>


    <div class="refsect1 parameters" id="refsect1-function.dns-get-record-parameters">
        <h3 class="title">参数</h3>
        <p class="para">
            <dl>

                
                    <dt>
<code class="parameter">hostname</code></dt>

                    <dd>

                        <p class="para">
                            主机名（<code class="parameter">hostname</code>）应该是一个DNS解析生效的域名，例如“<em>www.example.com</em>”。主机名也可以是通过对逆向解析域做DNS逆向域名解析而得到，但是在大多数情况下<span class="function"><a href="gethostbyaddr.html" class="function">gethostbyaddr()</a></span>更加适合做逆向域名解析。
                        </p>
                        <blockquote class="note"><p><strong class="note">Note</strong>: 
                            <p class="para">
                                每个DNS标准，邮件地址必须是<em>user.host</em>这样的格式（例如<em>hostmaster.example.com</em>而不是<em>hostmaster@example.com</em>），在使用<span class="function"><a href="mail.html" class="function">mail()</a></span>这个函数之前请检查这个值，有必要的话还需要修改。
                            </p>
                        </p></blockquote>
                    </dd>

                
                
                    <dt>
<code class="parameter">type</code></dt>

                    <dd>

                        <p class="para">
                            默认情况下，<span class="function"><strong>dns_get_record()</strong></span>将会搜索所有与<code class="parameter">hostname</code>相关的记录，可以通过设置<code class="parameter">type</code>来限定查询。<code class="parameter">type</code>的值可以是下面的其中的任何一个： <strong><code>DNS_A</code></strong>，<strong><code>DNS_CNAME</code></strong>，<strong><code>DNS_HINFO</code></strong>，<strong><code>DNS_MX</code></strong>，<strong><code>DNS_NS</code></strong>，<strong><code>DNS_PTR</code></strong>，<strong><code>DNS_SOA</code></strong>，<strong><code>DNS_TXT</code></strong>，<strong><code>DNS_AAAA</code></strong>，<strong><code>DNS_SRV</code></strong>，<strong><code>DNS_NAPTR</code></strong>，<strong><code>DNS_A6</code></strong>，<strong><code>DNS_ALL</code></strong>或者<strong><code>DNS_ANY</code></strong>。
                        </p>
                        <blockquote class="note"><p><strong class="note">Note</strong>: 
                            <p class="para">
                                由于dns在各个平台上表现有些不一样，<strong><code>DNS_ANY</code></strong>不会总是返回所有的记录，<strong><code>DNS_ALL</code></strong>虽然慢一些，但是会得到所有的记录，所以使用DNS_ALL更加可靠些。
                            </p>
                        </p></blockquote>
                    </dd>

                
                
                    <dt>
<code class="parameter">authns</code></dt>

                    <dd>

                        <p class="para">
                            以引用方式传递，如果写了该参数，那么将会得到该解析记录的DNS服务器（<em class="emphasis">Authoritative Name Servers</em>）的信息。
                        </p>
                    </dd>

                
                
                    <dt>
<code class="parameter">addtl</code></dt>

                    <dd>

                        <p class="para">
                            以引用方式传递，如果填写了该参数，将会得到<em class="emphasis">其他所有的DNS解析记录</em>。
                        </p>
                    </dd>

                
                
                    <dt>
<code class="parameter">raw</code></dt>

                    <dd>

                        <p class="para">
                            在原生模式下，在进行额外的查询的时候之前我们只执行请求的DNS类型，而不是循环查询所有的类型。
                        </p>
                    </dd>

                
            </dl>

        </p>
    </div>


    <div class="refsect1 returnvalues" id="refsect1-function.dns-get-record-returnvalues">
        <h3 class="title">返回值</h3>
        <p class="para">
            这个函数返回一个关联数组，如果失败则  或者在失败时返回 <strong><code>FALSE</code></strong>。每个关联数组都至少包含了以下的这些键。
            <em class="emphasis">at minimum</em> the following keys:
            <table class="doctable table">
                <caption><strong>Basic DNS attributes</strong></caption>
                
                    <thead>
                        <tr>
                            <th>Attribute</th>
                            <th>Meaning</th>
                        </tr>

                    </thead>

                    <tbody class="tbody">
                        <tr>
                            <td>host</td>
                            <td>
                                The record in the DNS namespace to which the rest of the associated data refers.
                            </td>
                        </tr>

                        <tr>
                            <td>class</td>
                            <td>
                                <span class="function"><strong>dns_get_record()</strong></span> only returns Internet class records and as
                                such this parameter will always return <em>IN</em>.
                            </td>
                        </tr>

                        <tr>
                            <td>type</td>
                            <td>
                                String containing the record type.  Additional attributes will also be contained
                                in the resulting array dependant on the value of type. See table below.
                            </td>
                        </tr>

                        <tr>
                            <td>ttl</td>
                            <td>
                                <em>&quot;Time To Live&quot;</em> remaining for this record. This will <em class="emphasis">not</em> equal
                                the record&#039;s original ttl, but will rather equal the original ttl minus whatever
                                length of time has passed since the authoritative name server was queried.
                            </td>
                        </tr>

                    </tbody>
                
            </table>

        </p>
        <p class="para">
            <table class="doctable table">
                <caption><strong>Other keys in associative arrays dependant on &#039;type&#039;</strong></caption>
                
                    <thead>
                        <tr>
                            <th>Type</th>
                            <th>Extra Columns</th>
                        </tr>

                    </thead>

                    <tbody class="tbody">
                        <tr>
                            <td><em>A</em></td>
                            <td>
                                <em>ip</em>: An IPv4 addresses in dotted decimal notation.
                            </td>
                        </tr>

                        <tr>
                            <td><em>MX</em></td>
                            <td>
                                <em>pri</em>: Priority of mail exchanger.
                                Lower numbers indicate greater priority.
                                <em>target</em>: FQDN of the mail exchanger.
                                See also <span class="function"><a href="dns_get_mx.html" class="function">dns_get_mx()</a></span>.
                            </td>
                        </tr>

                        <tr>
                            <td><em>CNAME</em></td>
                            <td>
                                <em>target</em>: FQDN of location in DNS namespace to which
                                the record is aliased.
                            </td>
                        </tr>

                        <tr>
                            <td><em>NS</em></td>
                            <td>
                                <em>target</em>: FQDN of the name server which is authoritative
                                for this hostname.
                            </td>
                        </tr>

                        <tr>
                            <td><em>PTR</em></td>
                            <td>
                                <em>target</em>: Location within the DNS namespace to which
                                this record points.
                            </td>
                        </tr>

                        <tr>
                            <td><em>TXT</em></td>
                            <td>
                                <em>txt</em>: Arbitrary string data associated with this record.
                            </td>
                        </tr>

                        <tr>
                            <td><em>HINFO</em></td>
                            <td>
                                <em>cpu</em>: IANA number designating the CPU of the machine
                                referenced by this record.
                                <em>os</em>: IANA number designating the Operating System on
                                the machine referenced by this record.
                                See IANA&#039;s <a href="http://www.iana.org/assignments/operating-system-names" class="link external">&raquo;&nbsp;<em>Operating System
                                Names</em></a> for the meaning of these values.
                            </td>
                        </tr>

                        <tr>
                            <td><em>SOA</em></td>
                            <td>
                                <em>mname</em>: FQDN of the machine from which the resource
                                records originated.
                                <em>rname</em>: Email address of the administrative contain
                                for this domain.
                                <em>serial</em>: Serial # of this revision of the requested
                                domain.
                                <em>refresh</em>: Refresh interval (seconds) secondary name
                                servers should use when updating remote copies of this domain.
                                <em>retry</em>: Length of time (seconds) to wait after a
                                failed refresh before making a second attempt.
                                <em>expire</em>: Maximum length of time (seconds) a secondary
                                DNS server should retain remote copies of the zone data without a
                                successful refresh before discarding.
                                <em>minimum-ttl</em>: Minimum length of time (seconds) a
                                client can continue to use a DNS resolution before it should request
                                a new resolution from the server.  Can be overridden by individual
                                resource records.
                            </td>
                        </tr>

                        <tr>
                            <td><em>AAAA</em></td>
                            <td>
                                <em>ipv6</em>: IPv6 address
                            </td>
                        </tr>

                        <tr>
                            <td><em>A6</em>(PHP &gt;= 5.1.0)</td>
                            <td>
                                <em>masklen</em>: Length (in bits) to inherit from the target
                                specified by <code class="parameter">chain</code>.
                                <em>ipv6</em>: Address for this specific record to merge with
                                <code class="parameter">chain</code>.
                                <em>chain</em>: Parent record to merge with
                                <code class="parameter">ipv6</code> data.
                            </td>
                        </tr>

                        <tr>
                            <td><em>SRV</em></td>
                            <td>
                                <em>pri</em>: (Priority) lowest priorities should be used first.
                                <em>weight</em>: Ranking to weight which of commonly prioritized
                                <code class="parameter">targets</code> should be chosen at random.
                                <em>target</em> and <em>port</em>: hostname and port
                                where the requested service can be found.
                                For additional information see: <a href="http://www.faqs.org/rfcs/rfc2782" class="link external">&raquo;&nbsp;RFC 2782</a>
                            </td>
                        </tr>

                        <tr>
                            <td><em>NAPTR</em></td>
                            <td>
                                <em>order</em> and <em>pref</em>: Equivalent to
                                <code class="parameter">pri</code> and <code class="parameter">weight</code> above.
                                <em>flags</em>, <em>services</em>, <em>regex</em>,
                                and <em>replacement</em>: Parameters as defined by
                                <a href="http://www.faqs.org/rfcs/rfc2915" class="link external">&raquo;&nbsp;RFC 2915</a>.
                            </td>
                        </tr>

                    </tbody>
                
            </table>

        </p>
    </div>


    <div class="refsect1 changelog" id="refsect1-function.dns-get-record-changelog">
        <h3 class="title">更新日志</h3>
        <p class="para">
            <table class="doctable informaltable">
                
                    <thead>
                        <tr>
                            <th>版本</th>
                            <th>说明</th>
                        </tr>

                    </thead>

                    <tbody class="tbody">
                        <tr>
                            <td>5.4.0</td>
                            <td>
                                增加了参数<code class="parameter">raw</code>。
                            </td>
                        </tr>

                        <tr>
                            <td>5.3.0</td>
                            <td>
                                可以是在windows平台上使用这个函数了。
                            </td>
                        </tr>

                        <tr>
                            <td>5.3.0</td>
                            <td>
                                在此版本之前，如果给参数<code class="parameter">authns</code>传入值，则必须同时传入<code class="parameter">addtl</code>的值。
                            </td>
                        </tr>

                    </tbody>
                
            </table>

        </p>
    </div>


    <div class="refsect1 examples" id="refsect1-function.dns-get-record-examples">
        <h3 class="title">范例</h3>
        <p class="para">
            <div class="example" id="example-5480">
                <p><strong>Example #1 使用 <span class="function"><strong>dns_get_record()</strong></span>函数</strong></p>
                <div class="example-contents">
                    <div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dns_get_record</span><span style="color: #007700">(</span><span style="color: #DD0000">"php.net"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
                </div>

                <div class="example-contents"><p>以上例程的输出类似于：</p></div>
                <div class="example-contents screen">
                    <div class="cdata"><pre>
Array
(
    [0] =&gt; Array
        (
            [host] =&gt; php.net
            [type] =&gt; MX
            [pri] =&gt; 5
            [target] =&gt; pair2.php.net
            [class] =&gt; IN
            [ttl] =&gt; 6765
        )

    [1] =&gt; Array
        (
            [host] =&gt; php.net
            [type] =&gt; A
            [ip] =&gt; 64.246.30.37
            [class] =&gt; IN
            [ttl] =&gt; 8125
        )

)
</pre></div>
                </div>
            </div>
            <div class="example" id="example-5481">
                <p><strong>Example #2 使用<span class="function"><strong>dns_get_record()</strong></span>配合DNS_ANY的例子</strong></p>
                <div class="example-contents"><p>
                    由于我们经常会想获取一个邮件服务器的对应的IP地址的MX记录是否已经生效。在使用<span class="function"><strong>dns_get_record()</strong></span>函数之后，<code class="parameter">addtl</code>能够返回一个相关的数组记录，<code class="parameter">authns</code>参数则会返回授权服务器的列表信息。
                </p></div>
                <div class="example-contents">
                    <div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Request&nbsp;"ANY"&nbsp;record&nbsp;for&nbsp;php.net,<br />&nbsp;&nbsp;&nbsp;and&nbsp;create&nbsp;$authns&nbsp;and&nbsp;$addtl&nbsp;arrays<br />&nbsp;&nbsp;&nbsp;containing&nbsp;list&nbsp;of&nbsp;name&nbsp;servers&nbsp;and<br />&nbsp;&nbsp;&nbsp;any&nbsp;additional&nbsp;records&nbsp;which&nbsp;go&nbsp;with<br />&nbsp;&nbsp;&nbsp;them&nbsp;*/<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dns_get_record</span><span style="color: #007700">(</span><span style="color: #DD0000">"php.net"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">DNS_ANY</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$authns</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$addtl</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Result&nbsp;=&nbsp;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Auth&nbsp;NS&nbsp;=&nbsp;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$authns</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Additional&nbsp;=&nbsp;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$addtl</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
                </div>

                <div class="example-contents"><p>以上例程的输出类似于：</p></div>
                <div class="example-contents screen">
                    <div class="cdata"><pre>
Result = Array
(
    [0] =&gt; Array
        (
            [host] =&gt; php.net
            [type] =&gt; MX
            [pri] =&gt; 5
            [target] =&gt; pair2.php.net
            [class] =&gt; IN
            [ttl] =&gt; 6765
        )

    [1] =&gt; Array
        (
            [host] =&gt; php.net
            [type] =&gt; A
            [ip] =&gt; 64.246.30.37
            [class] =&gt; IN
            [ttl] =&gt; 8125
        )

)
Auth NS = Array
(
    [0] =&gt; Array
        (
            [host] =&gt; php.net
            [type] =&gt; NS
            [target] =&gt; remote1.easydns.com
            [class] =&gt; IN
            [ttl] =&gt; 10722
        )

    [1] =&gt; Array
        (
            [host] =&gt; php.net
            [type] =&gt; NS
            [target] =&gt; remote2.easydns.com
            [class] =&gt; IN
            [ttl] =&gt; 10722
        )

    [2] =&gt; Array
        (
            [host] =&gt; php.net
            [type] =&gt; NS
            [target] =&gt; ns1.easydns.com
            [class] =&gt; IN
            [ttl] =&gt; 10722
        )

    [3] =&gt; Array
        (
            [host] =&gt; php.net
            [type] =&gt; NS
            [target] =&gt; ns2.easydns.com
            [class] =&gt; IN
            [ttl] =&gt; 10722
        )

)
Additional = Array
(
    [0] =&gt; Array
        (
            [host] =&gt; pair2.php.net
            [type] =&gt; A
            [ip] =&gt; 216.92.131.5
            [class] =&gt; IN
            [ttl] =&gt; 6766
        )

    [1] =&gt; Array
        (
            [host] =&gt; remote1.easydns.com
            [type] =&gt; A
            [ip] =&gt; 64.39.29.212
            [class] =&gt; IN
            [ttl] =&gt; 100384
        )

    [2] =&gt; Array
        (
            [host] =&gt; remote2.easydns.com
            [type] =&gt; A
            [ip] =&gt; 212.100.224.80
            [class] =&gt; IN
            [ttl] =&gt; 81241
        )

    [3] =&gt; Array
        (
            [host] =&gt; ns1.easydns.com
            [type] =&gt; A
            [ip] =&gt; 216.220.40.243
            [class] =&gt; IN
            [ttl] =&gt; 81241
        )

    [4] =&gt; Array
        (
            [host] =&gt; ns2.easydns.com
            [type] =&gt; A
            [ip] =&gt; 216.220.40.244
            [class] =&gt; IN
            [ttl] =&gt; 81241
        )

)
</pre></div>
                </div>
            </div>
        </p>
    </div>


    <div class="refsect1 notes" id="refsect1-function.dns-get-record-notes">
        <h3 class="title">注释</h3>
        <blockquote class="note"><p><strong class="note">Note</strong>: 
            <p class="para">
                For compatibility with versions before PHP 5.3.0 on some operating systems,
                try the <a href="http://pear.php.net/" class="link external">&raquo;&nbsp;PEAR</a> class
                <a href="http://pear.php.net/package/Net_DNS" class="link external">&raquo;&nbsp;Net_DNS</a>.
            </p>
        </p></blockquote>
    </div>


    <div class="refsect1 seealso" id="refsect1-function.dns-get-record-seealso">
        <h3 class="title">参见</h3>
        <p class="para">
            <ul class="simplelist">
                <li class="member"><span class="function"><a href="dns_get_mx.html" class="function" rel="rdfs-seeAlso">dns_get_mx()</a> - 别名 getmxrr</span></li>
                <li class="member"><span class="function"><a href="dns_check_record.html" class="function" rel="rdfs-seeAlso">dns_check_record()</a> - 别名 checkdnsrr</span></li>
            </ul>
        </p>
    </div>


</div></div></div></body></html>